//
//  RJCStationLine.m
//  AirQuality
//
//  Created by Po on 15/8/31.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCStationLine.h"
#import "SHLineGraphView.h"
#import "SHPlot.h"
@interface RJCStationLine ()

@property (retain, nonatomic) SHLineGraphView * lineView;
@property (retain, nonatomic) NSString * keyString;             //具体空气数据
@property (retain, nonatomic) NSArray * airData;                //每小时平均值
@property (retain, nonatomic) NSArray * aveData;                //城市平均值
@property (retain, nonatomic) NSArray * deviationData;          //偏移数值
@property (retain, nonatomic) NSArray * timeArray;              //最近24小时
@property (assign, nonatomic) CGFloat maxHeight;
@property (strong, nonatomic) NSArray * allArray;

- (void)initInterface;
@end
@implementation RJCStationLine

- (instancetype)initWithFrame:(CGRect)frame data:(NSDictionary *)data maxHeight:(CGFloat)height
{
    self = [super initWithFrame:frame];
    if (self) {
        if ([data count] != 0) {
            _keyString = [data[@"data"][0] allKeys][0];
            NSArray * tempData = data[@"data"];
            _airData = [NSArray arrayWithArray:[tempData[0] allValues][0]];
            _aveData = [NSArray arrayWithArray:[tempData[1] allValues][0]];
            _deviationData = [NSArray arrayWithArray:[tempData[2] allValues][0]];
            _allArray = @[_deviationData,_airData, _aveData];
            _timeArray = [NSArray arrayWithArray:data[@"times"]];
            _maxHeight = height;
        }
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    if (_keyString) {
        [self setContentSize:CGSizeMake(1000, self.bounds.size.height)];
        [self setContentOffset:CGPointMake(0, 0)];
        [self addSubview:[self getLineView]];
        [_lineView setupTheView];
    } else {
        _showStatusLabel = [[UILabel alloc] initWithFrame:self.bounds];
        [_showStatusLabel setText:@"正在获取数据，请稍等！"];
        [_showStatusLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [_showStatusLabel setTextColor:[UIColor blackColor]];
        [_showStatusLabel setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:_showStatusLabel];
    }
}

- (SHLineGraphView *)getLineView
{
    if (!_lineView) {
        CGRect rect = CGRectZero;
        rect.size = self.contentSize;
        _lineView = [[SHLineGraphView alloc] initWithFrame:rect];
        _lineView.barChartShowNum = YES;
        _lineView.AirType = [RJTools getTrueTypeTitlleWithCellKey:_keyString];
        if ([_lineView.AirType isEqualToString:@"PM2.5"]) {
            _lineView.AirType = @"PM2_5";
        }
        
        [self initLineView];
    }
    return _lineView;
}

///**初始化折线图*/
- (void)initLineView1
{
    if (_airData.count == 0) {
        return;
    }
    _lineView.barChartAlpha = 1;
    _lineView.barChartShowNum = YES;
    _lineView.barChartWidth = 10;
    NSDictionary *_themeAttributes = @{
                                       kXAxisLabelColorKey : GetRGBColor(85, 85, 85, 1),
                                       kXAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:12],
                                       kYAxisLabelColorKey : GetRGBColor(85, 85, 85, 1),
                                       kYAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:12],
                                       kYAxisLabelSideMarginsKey : @10,
                                       kPlotBackgroundLineColorKye : GetRGBColor(85, 85, 85, 0.2)
                                       };
    _lineView.themeAttributes = _themeAttributes;
    _lineView.yAxisRange = @(_maxHeight);
    
    
    
    _lineView.yAxisSuffix = @"ug";
    NSMutableArray * xLabelArray = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < _timeArray.count ; i ++) {
        [xLabelArray addObject:@{@(i + 1):_timeArray[i]}];
    }
    _lineView.xAxisValues = xLabelArray;
    
    //线条
    SHPlot *_plot1 = [[SHPlot alloc] init];
    //转化数据
    NSMutableArray * dataArray = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < 24; i ++) {
        if (i < _airData.count) {
            [dataArray addObject:@{@(i):_airData[i]}];
        } else {
            [dataArray addObject:@{@(i):@(0)}];
        }
    }
    _plot1.plottingValues = dataArray;
    
    SHPlot * _plot2 = [[SHPlot alloc] init];
    NSMutableArray * dataArray1 = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < 24; i ++) {
        if (i < _airData.count) {
            [dataArray1 addObject:@{@(i):_aveData[i]}];
        } else {
            [dataArray1 addObject:@{@(i ):@(0)}];
        }
        
    }
    _plot2.plottingValues = dataArray1;
    
    //偏移线条
    SHPlot * _plot3 = [[SHPlot alloc] init];
    NSMutableArray * dataArray2 = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < 24; i ++) {
        if (i < _airData.count) {
            [dataArray2 addObject:@{@(i):_deviationData[i]}];
        } else {
            [dataArray2 addObject:@{@(i):@(0)}];
        }
        
    }
    _plot3.plottingValues = dataArray2;

    //公共设置
    NSMutableArray * labelsTitle = [NSMutableArray arrayWithCapacity:0];
    for (NSNumber * number in _airData) {
        if ([_keyString isEqualToString:@"co"]) {
            [labelsTitle addObject:[NSString stringWithFormat:@"%.2f",[number floatValue]]];
        } else {
            [labelsTitle addObject:[NSString stringWithFormat:@"%ld",(long)[number integerValue]]];
        }
    }
    _plot1.plottingPointsLabels = labelsTitle;
    _plot2.plottingPointsLabels = labelsTitle;
    _plot3.plottingPointsLabels = labelsTitle;
    
    NSDictionary *_plotThemeAttributes1 = @{
                                           kPlotFillColorKey : [UIColor clearColor],
                                           kPlotStrokeWidthKey : @2,
                                           kPlotStrokeColorKey : SysMainColor,
                                           kPlotPointFillColorKey : SysMainColor,
                                           kPlotPointValueFontKey : [UIFont fontWithName:@"TrebuchetMS" size:18]
                                           };
    
    NSDictionary *_plotThemeAttributes2 = @{
                                            kPlotFillColorKey : [UIColor clearColor],
                                            kPlotStrokeWidthKey : @2,
                                            kPlotStrokeColorKey : GetRGBColor(236, 93, 78, 1),
                                            kPlotPointFillColorKey : GetRGBColor(236, 93, 78, 1),
                                            kPlotPointValueFontKey : [UIFont fontWithName:@"TrebuchetMS" size:18]
                                            };
    NSDictionary *_plotThemeAttributes3 = @{
                                            kPlotFillColorKey : [UIColor clearColor],
                                            kPlotStrokeWidthKey : @2,
                                            kPlotStrokeColorKey : [UIColor clearColor],
                                            kPlotPointFillColorKey : [UIColor clearColor],
                                            kPlotPointValueFontKey : [UIFont fontWithName:@"TrebuchetMS" size:18]
                                            };
    
    _plot1.plotThemeAttributes = _plotThemeAttributes1;
    _plot2.plotThemeAttributes = _plotThemeAttributes2;
    _plot3.plotThemeAttributes = _plotThemeAttributes3;
    [_lineView addPlot:_plot3];
    [_lineView addPlot:_plot2];
    [_lineView addPlot:_plot1];
}

/**初始化折线图*/
- (void)initLineView
{
    if (_airData.count == 0) {
        return;
    }
    _lineView.barChartAlpha = 1;
    _lineView.barChartShowNum = YES;
    _lineView.barChartWidth = 5;
    NSDictionary *_themeAttributes = @{
                                       kXAxisLabelColorKey : GetRGBColor(85, 85, 85, 1),
                                       kXAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:12],
                                       kYAxisLabelColorKey : GetRGBColor(85, 85, 85, 1),
                                       kYAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:12],
                                       kYAxisLabelSideMarginsKey : @10,
                                       kPlotBackgroundLineColorKye : GetRGBColor(85, 85, 85, 0.2)
                                       };
    _lineView.themeAttributes = _themeAttributes;
    _lineView.yAxisRange = @(_maxHeight);
    
    
    
    _lineView.yAxisSuffix = @"ug";
    
    
    NSMutableArray * xLabelArray = [[NSMutableArray alloc] init];
    for (NSInteger i =  _timeArray.count-1; i >= 0; i --) {
        [xLabelArray addObject:@{@(_timeArray.count-1-i):_timeArray[i]}];
    }
    _lineView.xAxisValues = xLabelArray;
    //转化数据为折线图数据
//    NSArray * dataArray = [self updateData:_airData];
    for (NSUInteger i = 0; i < _allArray.count; i ++) {
        NSArray * lineData = _allArray[i];
        SHPlot *_plot1 = [[SHPlot alloc] init];
        //转化数据
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        for (NSInteger i =  _timeArray.count-1; i >= 0; i --) {
            [dataArray addObject:@{@(_timeArray.count-1-i):lineData[i]}];
        }
        _plot1.plottingValues = dataArray;
        
        NSMutableArray * labelsTitle = [NSMutableArray arrayWithCapacity:0];
        for (NSNumber * number in lineData) {
            if ([_keyString isEqualToString:@"CO"]) {
                [labelsTitle addObject:[NSString stringWithFormat:@"%.2f",[number floatValue]]];
            } else {
                [labelsTitle addObject:[NSString stringWithFormat:@"%ld",[number integerValue]]];
            }
        }
        _plot1.plottingPointsLabels = labelsTitle;
        
        NSDictionary *_plotThemeAttributes = @{
                                               kPlotFillColorKey : [RJTools getCstationColors][i],
                                               kPlotStrokeWidthKey : @2,
                                               kPlotStrokeColorKey : [RJTools getCstationColors][i],
                                               kPlotPointFillColorKey : [RJTools getCstationColors][i],
                                               kPlotPointValueFontKey : [UIFont fontWithName:@"TrebuchetMS" size:18]
                                               };
        
        _plot1.plotThemeAttributes = _plotThemeAttributes;
        [_lineView addPlot:_plot1];
    }
    
}

/**更新数据*/
- (NSArray *)updateData:(NSArray *)data
{
    NSMutableArray * tempData = [NSMutableArray array];
    for (NSDictionary * dic in data) {
        [tempData addObject:[[dic allValues] lastObject]];
    }
    return tempData;
}
@end
